package com.ourteam.frame.web;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.lang.time.DateFormatUtils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class CustomObjectMapper extends ObjectMapper {

	private static final String DATE_PATTERN = "yyyy-MM-dd";

	private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public CustomObjectMapper() {
		

		SimpleModule module = new SimpleModule();
		
		//module.addSerializer(type, ser)
		
		module.addSerializer(Date.class, new JsonSerializer<Date>() {
			@Override
			public void serialize(Date value, JsonGenerator jsonGenerator,
					SerializerProvider provider) throws IOException,
					JsonProcessingException {

				String dataStr = DateFormatUtils
						.format(value, DATETIME_PATTERN);

				if (dataStr.endsWith("00:00:00")) {
					dataStr = DateFormatUtils.format(value, DATE_PATTERN);
				}

				jsonGenerator.writeString(dataStr);
			}
		});

		module.addSerializer(Timestamp.class,
				new JsonSerializer<Timestamp>() {
					@Override
					public void serialize(Timestamp value,
							JsonGenerator jsonGenerator,
							SerializerProvider provider) throws IOException,
							JsonProcessingException {

						jsonGenerator.writeString(DateFormatUtils.format(value,
								DATETIME_PATTERN));
					}
				});
		
		module.addSerializer(BeanMap.class,
				new JsonSerializer<BeanMap>() {
					@Override
					public void serialize(BeanMap value,
							JsonGenerator jsonGenerator,
							SerializerProvider provider) throws IOException,
							JsonProcessingException {

						jsonGenerator.writeString("");
					}
				});
		
		this.registerModule(module);

	}

	
	
}
